<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="con"></div>
</body>
<script>
    //通过定义对象的方式实现一个组件
    //适用于在页面中使用一次，不会多次复用
    let btnObj = {
        init(config) {
            let con = document.querySelector('.con');
            let btn = document.createElement('button');
            let intervalid;
            let count = config.count;

            btn.innerHTML = config.text;

            this.intervalid = intervalid;
            this.count = count;
            this.btn = btn;
            btn.onclick = this.setCount.bind(this);

            config.con.appendChild(btn);
        },
        setCount() {
            if(this.intervalid) {
                clearInterval(this.intervalid);
                this.intervalid = '';
            }else{
                this.intervalid = setInterval(() => {
                    this.btn.innerHTML = this.count--;
                },1000)
            }
        }
    }

    btnObj.init({
        count:10,
        text:'start',
        con:document.querySelector('.con')
    })
</script>
</html>